//
//  ViewController.m
//  AliBall
//
//  Created by yh on 16/8/12.
//  Copyright © 2016年 yh. All rights reserved.
//
#import "FifCollectionViewCell.h"
#import "ViewController.h"
#import "AliXingqiu.h"
#import "Data.h"
#import "YmjLayout.h"
#import "FirstCollectionViewCell.h"
#import "SecCollectionViewCell.h"
#import "ThireCollectionViewCell.h"
#import "FourthCollectionViewCell.h"
#import "CircleCollectionView.h"
#define SECTION section + 2
#define INDEXPARNSECTION indexPath.section + 2
#define INDEXPATHROW indexPath.row - 1
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,CircleCollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collect;
@property (nonatomic, strong) AliXingqiu *ali;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createData];
    [self createView];
}
- (void)createView {
    YmjLayout *flowLayout = [[YmjLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(90, 90);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 10;
    _collect  = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    _collect.contentInset = UIEdgeInsetsMake(300, 0, 0, 0);
    [self.view addSubview:_collect];
    self.collect.delegate = self;
    self.collect.dataSource = self;
    // 5种类CELL
    [self.collect registerNib:[UINib nibWithNibName:@"FifCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"1"];
    [self.collect registerNib:[UINib nibWithNibName:@"FirstCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"2"];
    [self.collect registerNib:[UINib nibWithNibName:@"SecCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"3"];
    [self.collect registerNib:[UINib nibWithNibName:@"ThireCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"4"];
    [self.collect registerNib:[UINib nibWithNibName:@"FourthCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"5"];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _ali.data[SECTION].data_array.count + 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _ali.data.count - 2;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FirstCollectionViewCell *first_cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"2" forIndexPath:indexPath];
    SecCollectionViewCell *sec_cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"3" forIndexPath:indexPath];
    ThireCollectionViewCell *third_cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"4" forIndexPath:indexPath];
    FourthCollectionViewCell *four_cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"5" forIndexPath:indexPath];
    FifCollectionViewCell *fif_cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            first_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].name;
            return first_cell;
        } else {
            
            sec_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].name;
            sec_cell.label_ymj_sec.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].desc;
            return sec_cell;
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            
            third_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].name;
            return third_cell;
        }
        else {
            four_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].name;
            four_cell.label_ymj_sec.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].desc;
            four_cell.imageView_ymj.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].picUrl]]];
            return four_cell;
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            first_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].name;
            return first_cell;
        }
        else {
            four_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].name;
            four_cell.label_ymj_sec.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].desc;
            four_cell.imageView_ymj.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].picUrl]]];
            return four_cell;
        }
    } else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            third_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].name;
            return third_cell;
        }
        else {
            four_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].name;
            four_cell.label_ymj_sec.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].desc;
            four_cell.imageView_ymj.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].picUrl]]];
            return four_cell;
        }
    } else if (indexPath.section == 4) {
        if (indexPath.row == 0) {
            first_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].name;
            return first_cell;
        }
        else {
            four_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].name;
            four_cell.label_ymj_sec.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].desc;
            four_cell.imageView_ymj.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].picUrl]]];
            return four_cell;
        }
    } else {
        
        if (indexPath.row == 0) {
            third_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].name;
            return third_cell;
        }
        else {
            fif_cell.label_ymj.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].name;
            fif_cell.label_ymj_sec.text = _ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].desc;
            fif_cell.imageView_ymj.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_ali.data[INDEXPARNSECTION].data_array[indexPath.row - 1].picUrl]]];
            return fif_cell;
        }
        
    }
}
- (void)createData {
    NSString *urlStr = @"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8";
    NSString *secUrlStr = @"%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400";
    NSString *stringUrl = [urlStr stringByAppendingString:secUrlStr];
    NSURL *url = [NSURL URLWithString:stringUrl];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.ali = [AliXingqiu modelWithDic:dic];
                    CircleCollectionView *circle = [[CircleCollectionView alloc] initWithFrame:CGRectMake(0, -300, 320, 200) imageArray:_ali.data[0].data_array];
                    [_collect addSubview:circle];
                    circle.dataSource = self;
            [_collect reloadData];
        });
        
    }];
    [task resume];
    
    
}
// 协议方法
- (void)handleImageView:(CustomCollectionViewCell *)cell objc:(id)objc {
    Data1 *data1 = objc;
    cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:data1.picUrl]]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
